package com.byt.framlib.commonwidget;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

import com.byt.framlib.R;

/**
 * @Author: Bro0cL
 * @Date: 2017/12/27 16:25
 * 图片和文字居中的RadioButton，只处理左边或上边有图片的情况，
 * 可以启用默认的缩放动画：在手指按下时会有缩小动画，在手指离开时有复原动画
 */

@SuppressLint("AppCompatCustomView")
public class TabRadioView extends RelativeLayout {
    /**
     * 默认缩放动画，缩放比例为3/4
     */
    private static final float DEFAULT_SCALE_RATE = 0.75f;
    /**
     * 默认缩放动画的时长
     */
    private static final int DEFAULT_DURATION = 200;
    /**
     * 如果drawable的size为-1，则显示其默认大小
     */
    private static final int DEFAULT_DRAWABLE_SIZE = -1;

    /**
     * 是否启用动画效果
     */
    private boolean enableAnimation;
    /**
     * 手指按下时的动画
     */
    private Animator pressedAnimator;
    /**
     * 手指离开时的动画
     */
    private Animator releasedAnimator;
    private long duration;
    private float scaleRate;

    public TabRadioView(Context context) {
        this(context, null);
    }

    public TabRadioView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TabRadioView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        parseAttrs(context, attrs);
        init();
    }

    private void parseAttrs(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TabRadioView);
        scaleRate = array.getFloat(R.styleable.TabRadioView_trb_scale_rate, DEFAULT_SCALE_RATE);
        duration = array.getInteger(R.styleable.TabRadioView_trb_duration, DEFAULT_DURATION);
        enableAnimation = array.getBoolean(R.styleable.TabRadioView_trb_enable_animation, false);
        array.recycle();
    }

    private void init() {
        setClickable(true);
        setFocusable(true);
        //初始化动画
        pressedAnimator = getAnimator(true);
        releasedAnimator = getAnimator(false);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //手指按下时开始动画
                if (enableAnimation && !pressedAnimator.isRunning()) {
                    pressedAnimator.start();
                }
                break;
            case MotionEvent.ACTION_UP:
                //手指离开时进行复原
                if (enableAnimation && !releasedAnimator.isRunning()) {
                    releasedAnimator.start();
                }
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    /**
     * @param whenPressed 是否是按下时的动画
     * @return
     */
    private Animator getAnimator(boolean whenPressed) {
        return getDefaultScaleAnimator(whenPressed ? 1.0f : scaleRate,
                whenPressed ? scaleRate : 1.0f, duration);
    }

    private Animator getDefaultScaleAnimator(float from, float to, long duration) {
        AnimatorSet animatorSet = new AnimatorSet();
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(this, "ScaleX", from, to);
        ObjectAnimator animatorY = ObjectAnimator.ofFloat(this, "ScaleY", from, to);
        animatorSet.play(animatorX).with(animatorY);
        animatorSet.setDuration(duration);
        animatorSet.setTarget(this);
        return animatorSet;
    }

    public void setEnableAnimation(boolean enableAnimation) {
        this.enableAnimation = enableAnimation;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    public void setScaleRate(float scaleRate) {
        this.scaleRate = scaleRate;
    }
}